package com.taritari.journal.controller;

import com.taritari.journal.entity.dto.DatesDto;
import com.taritari.journal.entity.dto.JournalAddDto;
import com.taritari.journal.entity.dto.JournalsDto;
import com.taritari.journal.entity.po.Journals;
import com.taritari.journal.entity.vo.JournalDetailVo;
import com.taritari.journal.entity.vo.PagesVo;
import com.taritari.journal.entity.vo.TodayJournalVo;
import com.taritari.journal.services.JournalsService;
import com.taritari.journal.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @author taritari
 * @date 2024-01-08 20:58
 * @description
 */
@RestController
@Slf4j
@Tag(name = "日记列表", description = "日记的操作")
@RequestMapping("/journals")
public class JournalsController {
    @Autowired
    private JournalsService journalsService;
    @PostMapping("/getJournals")
    @Operation(summary = "获取日记列表", description = "传入分页数据")
    public Result getJournalByAuthorId(@RequestBody PagesVo pagesVo){
        JournalsDto journalByAuthorId = journalsService.getJournalByAuthorId(pagesVo.getId(), pagesVo.getCurrentPage());
        return Result.success(journalByAuthorId);
    }
    @GetMapping("/getDetail")
    @Operation(summary = "通过id获取日记详情", description = "通过id获取日记详情")
    public Result getJournalInfoById(@RequestParam int id){
        JournalDetailVo journalInfoById = journalsService.getJournalInfoById(id);
        return Result.success(journalInfoById);
    }
    @GetMapping("/share")
    @Operation(summary = "分享日记-日记id", description = "分享日记-日记id")
    public Result shareJournal(@RequestParam int id){
        JournalDetailVo journalInfoById = journalsService.getShareJournal(id);
        return Result.success(journalInfoById);
    }
    @PostMapping("/getUserJournalState")
    @Operation(summary = "获取用户日记写作情况", description = "获取用户日记写作情况")
    public Result getUserJournalState(@RequestBody DatesDto datesDto){
        Result result = journalsService.selectUserWritingThisMonth(datesDto.getDates(), datesDto.getId());
        return result;
    }
    @GetMapping("/getJournalByDate")
    @Operation(summary = "通过日期获取日记详情", description = "通过日期获取日记详情")
    public Result getJournalByDate(@RequestParam String date,@RequestParam int id){
        Result result = journalsService.selectJournalDetailByDate(date, id);
        return result;
    }

    @PostMapping("/addJournal")
    @Operation(summary = "添加日记", description = "添加日记")
    public Result addJournal(@RequestBody JournalAddDto journalAddDto){
        Result result = journalsService.addJournal(journalAddDto);
        return result;
    }


    @GetMapping("/getToday")
    @Operation(summary = "通过日期获取当天的日记", description = "通过日期获取当天的日记")
    public Result getToday(@RequestParam String date,@RequestParam int id){
        TodayJournalVo todayJournalVo = journalsService.queryTodayJournal(id, date);
        return Result.success(todayJournalVo);
    }
    @GetMapping("/queryByDate")
    @Operation(summary = "通过id获取日记源数据-修改用", description = "通过id获取日记源数据-修改用")
    public Result queryByDate(@RequestParam int id,@RequestParam int authorId){
        TodayJournalVo todayJournalVo = journalsService.queryJournalByDate(id, authorId);
        return Result.success(todayJournalVo);
    }
    @GetMapping("/delete")
    @Operation(summary = "通过id删除日记", description = "通过id删除日记")
    public Result deleteById(@RequestParam int id){
        int res = journalsService.deleteJournalById(id);
        return Result.success(res);
    }
    @GetMapping("/retDate")
    @Operation(summary = "一个传入日期返回日期的接口", description = "一个传入日期返回日期的接口")
    public Result retDate(@RequestParam String date){
        return Result.success(date);
    }

    @GetMapping("/threeJournal")
    @Operation(summary = "查询指定用户的前三篇日记", description = "查询指定用户的前三篇日记")
    public Result threeJournal(@RequestParam int authorId){
        Map<String, Object> journals = journalsService.getThreeJournal(authorId);
        return Result.success(journals);
    }

}
